package com.chance.cc.crawler.prod.command.meta.autoHome;

import com.chance.cc.crawler.meta.core.bean.crawler.CrawlerDomainKeys;
import com.chance.cc.crawler.prod.command.meta.CommonCrawlerKeywordScript;
import com.chance.cc.crawler.prod.command.meta.CrawlerDomainKeywordCommand;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class koubei{

    private static final String HOST = "192.168.1.215";
    private static final int PORT = 9599;

    public static final CrawlerDomainKeywordCommand keywordCommand = new CrawlerDomainKeywordCommand(HOST,PORT);

    private static final String domain = "autohome";
    public static void main(String[] args) {
        realtimeKeyword();
    }

    private static void realtimeKeyword() {
        String site = "carSeriesKouBei";
        String filePath = "D:\\it\\idea\\chance-crawler-development\\crawler-scripts-command-prod\\src\\main\\java\\com\\chance\\cc\\crawler\\prod\\command\\meta\\autoHome\\koubei.txt";
        List<String> keywords = duplicateKeywords(filePath);
        addKeywords(site,keywords);
//        delKeywords(site,keywords);
    }

    private static void adhocKeyword() {
        String site = "adhocSeries";
        String filePath = "F:\\chance_work\\chance-crawler-development\\crawler-scripts-command-prod\\src\\main\\java\\com\\chance\\cc\\crawler\\prod\\command\\meta\\dcdapp\\adhoc_series.txt";
        List<String> keywords = duplicateKeywords(filePath);
        addKeywords(site,keywords);
//        delKeywords(site,keywords);
    }


    private static List<String> duplicateKeywords(String filePath){//复制关键字
        Set<String> duplicate = new HashSet<>(getContent(filePath));
        return new ArrayList<>(duplicate);
    }


    private static void addKeywords(String site,List<String> keywords){//添加关键字
        for (String keyword : keywords) {
            System.out.println(keyword);
            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain(domain);
            crawlerDomainKeys.setSite(site);
            crawlerDomainKeys.setKeyword(keyword);
            crawlerDomainKeys.setStatus(0);
            keywordCommand.addOrUpdateKeys(crawlerDomainKeys);
        }
    }


    private static void delKeywords(String site,List<String> keywords){
        for (String keyword : keywords) {
            System.out.println(keyword);
            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain(domain);
            crawlerDomainKeys.setSite(site);
            crawlerDomainKeys.setKeyword(keyword);
            crawlerDomainKeys.setStatus(0);
            keywordCommand.delKeys(crawlerDomainKeys);
        }
    }


    public static List<String> getContent(String filepath)  {//获取内容
        List<String> list =  new ArrayList<>();
        try{
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filepath)), "UTF-8"));
            String lineData = null;
            while ((lineData = bufferedReader.readLine()) != null){
                if(StringUtils.isBlank(lineData) || lineData.startsWith("#")){
                    continue;
                }
                list.add(lineData);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

}
